# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import csv


class RzyPipeline:
    def __init__(self):
        file_path = './data.csv'
        self.file = open(file_path, "a+", encoding="utf-8-sig", newline='')
        self.writer = csv.writer(self.file, dialect="excel")

    def process_item(self, item, spider):
        if item['file_name']:
            self.writer.writerow([item['file_name'], item['file_url'], item['file_time'], item['file_page1']])
        return item

    def close_spider(self, spider):
        self.file.close()
